package com.ilop.siteraudio.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ilop.siteraudio.R;
import com.ilop.siteraudio.data.bean.ReportBean;
import com.ilop.siteraudio.data.util.ReportDaoUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2/1/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class LocationFragment extends Fragment {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.local_num)
    EditText mReportNumber;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.local_input)
    EditText mReportLocal;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.local_comment)
    EditText mReportComment;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.local_save)
    Button mReportSave;

    private ReportBean mReportBean;

    public static LocationFragment newInstance(long reportId) {
        Bundle args = new Bundle();
        args.putLong("REPORT_ID", reportId);
        LocationFragment fragment = new LocationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_location, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        assert getArguments() != null;
        long reportId = getArguments().getLong("REPORT_ID");
        mReportBean = ReportDaoUtil.getInstance().getReportById(reportId);
        initData();
        initListener();
    }

    private void initData() {
        mReportNumber.setText(mReportBean.getReferenceNo());
        mReportLocal.setText(mReportBean.getLocation());
        mReportComment.setText(mReportBean.getComment());
    }

    private void initListener() {
        mReportSave.setOnClickListener(view -> {
            String number = mReportNumber.getText().toString();
            String local = mReportLocal.getText().toString();
            String comment = mReportComment.getText().toString();
            mReportBean.setReferenceNo(number);
            mReportBean.setLocation(local);
            mReportBean.setComment(comment);
            ReportDaoUtil.getInstance().getReportDao().insertOrReplace(mReportBean);
        });
    }
}
